<template>
  <common-list>
    <common-search slot="search" @search="handleQuery" @reset="resetQuery">
      <el-form
        @submit.native.prevent
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        @enter="handleQuery"
        label-width="37px"
      >
        <el-form-item label="标题" prop="noticeTitle">
          <common-input
            v-model="queryParams.noticeTitle"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status">
            <el-option label="全部" :value="null"></el-option>
            <el-option label="未发布" value="0"></el-option>
            <el-option label="已发布" value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </common-search>

    <el-row :gutter="10" class="mb8" slot="tools">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" @click="handleAdd"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          :disabled="ids.length < 1"
          @click="handleDelete"
          >批量删除
        </el-button>
      </el-col>
    </el-row>

    <common-table
      v-loading="loading"
      slot="content"
      :data="noticeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="标题"
        align="left"
        prop="noticeTitle"
        show-overflow-tooltip
      />
      <el-table-column
        label="发布时间"
        align="left"
        width="180"
        prop="updateTime"
      >
        <template slot-scope="scope">
          {{
            scope.row.updateTime
              ? parseTime(new Date(scope.row.updateTime))
              : ""
          }}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="left" prop="status" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === '1'" type="success"
            >已发布
          </el-tag>
          <el-tag v-if="scope.row.status === '0'" type="danger">未发布</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="left" width="125px">
        <template slot-scope="scope">
          <el-button
            type="text"
            v-if="scope.row.status === '0'"
            @click="handleRelease(scope.row)"
            :disabled="scope.row.releaseDisabled"
            >发布
          </el-button>

          <el-button
            type="text"
            v-if="scope.row.status === '1'"
            @click="handleDetail(scope.row)"
            >详情
          </el-button>

          <el-button
            type="text"
            v-if="scope.row.status === '0'"
            @click="handleAdd(scope.row)"
            >编辑
          </el-button>

          <el-button type="text" @click="handleDelete(scope.row)"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </common-table>

    <pagination
      slot="pagination"
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <notice-edit ref="noticeEdit" @submitForm="submitForm"></notice-edit>
    <notice-detail
      v-if="noticeDetailShow"
      @back="noticeDetailShow = false"
      :noticeId="noticeId"
    ></notice-detail>
  </common-list>
</template>

<script>
import {
  fetchList,
  noticeUpdateStatus,
  noticeDelete,
} from "@/api/system/publicNotice";
import NoticeEdit from "./components/noticeEdit.vue";
import BaseList from "@/mixins/BaseList";
import NoticeDetail from "./components/noticeDetail.vue";
export default {
  mixins: [BaseList],
  components: {
    NoticeEdit,
    NoticeDetail,
  },
  data() {
    return {
      ids: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        noticeTitle: null,
        status: null,
      },
      total: 0,
      noticeList: [],
      loading: false,
      noticeDetailShow: false,
      noticeId: "",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      fetchList(this.queryParams)
        .then((res) => {
          if (res && (res.code === 200 || res.rescode === 200)) {
            this.noticeList = res.data.rows.reduce((r, c) => {
              r.push({
                ...c,
                releaseDisabled: false,
              });
              return r;
            }, []);

            this.total = res.data.total;
          } else {
            this.noticeList = [];
            this.total = 0;
            this.handleListTips(res);
          }
        })
        .catch(() => {
          this.noticeList = [];
          this.total = 0;
        })
        .finally(() => {
          this.loading = false;
        });
    },

    handleAdd(row) {
      this.$refs.noticeEdit.show(row ? { ...row } : {});
    },

    handleRelease(row) {
      this.$GlobalDeleteConfirm("是否确认发布该条公告？", "提示")
        .then(() => {
          row.releaseDisabled = true;
          noticeUpdateStatus({ noticeId: row.noticeId, status: 1 })
            .then((res) => {
              if (res && res.code === 200) {
                //获取通知个数
                // this.$bus.$emit("messageList");
                this.$GlobalSuccessMessage("发布成功");
                this.getList();
              } else {
                this.$GlobalErrorConfirm("发布失败", res.msg);
              }
            })
            .finally(() => {
              row.releaseDisabled = false;
            });
        })
        .catch(() => {});
    },

    handleDetail(row) {
      this.noticeId = row.noticeId;
      this.noticeDetailShow = true;
    },

    handleDelete(row) {
      const ids = row.noticeId || this.ids.join(",");

      this.$GlobalDeleteConfirm()
        .then(() => {
          noticeDelete({ noticeId: ids })
            .then((res) => {
              if (res && res.code == 200) {
                this.$GlobalSuccessMessage("删除成功");
                if (
                  this.queryParams.pageNum > 1 &&
                  ids.split(",").length === this.noticeList.length
                ) {
                  this.queryParams.pageNum -= 1;
                }
                this.getList();
              } else {
                this.$GlobalErrorConfirm("删除失败", res.msg);
              }
            })
            .finally(() => {
              // this.$bus.$emit("messageListChange");
            });
        })
        .catch(() => {});
    },

    submitForm() {
      this.getList();
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.noticeId);
    },

    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.queryParams = this.$options.data().queryParams;
    },
  },
};
</script>

<style lang="scss"></style>
